
public class TradeOffer {

	private Player proposer;
	/*
	 * Offers and request arrays should use standard resource order: brick, grain, lumber, ore, wool.
	 * Final ints correspond to correct integer indexes
	 */
	private final int BRICK = 0;
	private final int GRAIN = 1;
	private final int LUMBER = 2;
	private final int ORE = 3;
	private final int WOOL = 4;
	private int[] offer;
	private int[] request;
	
	//Offer and request should be int[5]
	public TradeOffer (Player proposer, int[] offer, int[] request){
		this.proposer = proposer;
		this.offer =  offer;
		this.request = request;
	}

	/*
	 * Accepts the offer, creating a trade between @param Player p and the Player proposer. Checks that Player p 
	 * is able to accep the offer, and increment/decrements resources appropriately.
	 * Precondition: Player must have more of a given resource than the corresponding offer.
	 */
	public boolean acceptOffer(Player p){
		if (p.getBrick() >= request[0] && p.getGrain() >=  request[1] && p.getLumber() >= request[2] 
			&& p.getOre() >= request[3] && p.getWool() >= request[4]){
			p.addBrick(offer[BRICK] - request[BRICK]);
			proposer.addBrick(request[BRICK] - offer[BRICK]);
			p.addGrain(offer[GRAIN] - request[GRAIN]);
			proposer.addGrain(request[GRAIN] - offer[GRAIN]);
			p.addLumber(offer[LUMBER] - request[LUMBER]);
			proposer.addLumber(request[LUMBER] - offer[LUMBER]);
			p.addOre(offer[ORE] - request[ORE]);
			proposer.addOre(request[ORE] - offer[ORE]);
			p.addWool(offer[WOOL] - request[WOOL]);
			proposer.addWool(request[WOOL] - offer[WOOL]);
			return true;
		}
		return false;
	}
	/*
	 * Checks that player's offer fits requirements for sending to bank, and completes trade accordingly.
	 */
	public boolean sendToBank(){
		int numOfResources = 0;
		boolean oneCompleteTrade = false;
//		int target = -1;
		for (int i = 0; i < request.length; i++){
			if (request[i] != 0){
				numOfResources++;
				if (request[i] % 4 == 0 && request[i] / 4 == getOfferSum()){
//					target = i;
					oneCompleteTrade = true;
				}
			}
		}
		if (numOfResources == 1 && oneCompleteTrade){
			addResources();
			return true;
		}
		return false;
	}
	//Returns total number of resources in request array.
	private int getOfferSum() {
		// TODO Auto-generated method stub
		int total = 0;
		for (int i: request){
			total += i;
		}
		return total;
	}

	//Completes a trade without a player (proposer gets all resources appropriately)
	public boolean addResources(){
		proposer.addBrick(request[BRICK] - offer[BRICK]);
		proposer.addGrain(request[GRAIN] - offer[GRAIN]);
		proposer.addLumber(request[LUMBER] - offer[LUMBER]);
		proposer.addOre(request[ORE] - offer[ORE]);
		proposer.addWool(request[WOOL] - offer[WOOL]);
		return true;
	}
	
	/*
	 * Returns true if such an increase in resource offer is possible for proposer
	 * Int type represents which resource is being increased (in order listed above)
	 */
	public boolean addToOffer (int type, int increase){
		if (type == BRICK && proposer.getBrick() >= increase){
			offer[BRICK] += increase;
			return true;
		} else if (type == GRAIN && proposer.getGrain() >= increase){
			offer[GRAIN] += increase;
			return true;
		} else if (type == LUMBER && proposer.getLumber() >= increase){
			offer[LUMBER] += increase;
			return true;
		} else if (type == ORE && proposer.getOre() >= increase){
			offer[ORE]+= increase;
			return true;
		} else if (type == WOOL && proposer.getWool() >= increase){
			offer[WOOL] += increase;
			return true;
		}
		return false;
	}
	
	/*
	 * Adds @param increase number of resources of type @param type to the request 
	 */
	public boolean addToRequest (int type, int increase){
		request[type] += increase;
		return true;
	}
	
	public Player getProposer() {
		return proposer;
	}
	public void setProposer(Player proposer) {
		this.proposer = proposer;
	}
	public int[] getOffer() {
		return offer;
	}
	public void setOffer(int[] offer) {
		this.offer = offer;
	}
	public int[] getRequest() {
		return request;
	}
	public void setRequest(int[] request) {
		this.request = request;
	}
	
	
}
